using System;
using System.Collections.Generic;
using System.Configuration;
using Google.GData.Client;
using Google.GData.Extensions.Location;
using Google.GData.YouTube;
using Google.YouTube;
using Social;

namespace Logic.SocialNetworks
{

	public class YouTubeService
	{
		#region Readonly Variables
		private readonly string appName;
		private readonly string apiKey;
		#endregion

		#region Constructors
		public YouTubeService(string appName, string apiKey)
		{
			this.apiKey = apiKey;
			this.appName = appName;
		}
		#endregion

		#region Static Methods
		public static string GetEmbeddedString(string videoId)
		{
			return string.Format(@"<object width='200' height='150'>
				<param name='movie' value='http://www.youtube.com/v/{0}&hl=en_US&fs=1&' ></param>
				<param name='allowFullScreen' value='true'></param>
				<param name='allowscriptaccess' value='always'></param>
				<embed src='http://www.youtube.com/v/{0}&hl=en_US&fs=1&' type='application/x-shockwave-flash'
					allowscriptaccess='always' allowfullscreen='true' width='200' height='150'>
				</embed></object>", videoId);
		}
		public static string GetThumbnailUrl(string videoId)
		{
			return string.Format("http://i1.ytimg.com/vi/{0}/default.jpg", videoId);
		}

		public static string GetVideoUrl(string videoId)
		{
			return string.Format("http://www.youtube.com/watch?v={0}", videoId);
		}

		#endregion

		#region Methods
		/// <summary>
		/// Returns search results for queryStr
		/// </summary>
		/// <param name="queryStr"></param>
		/// <returns></returns>
		public IEnumerable<Video> SearchVideo(string queryStr)
		{
			var query = new YouTubeQuery(YouTubeQuery.DefaultVideoUri);
			query.OrderBy = "viewCount";
			query.Query = queryStr;
			query.SafeSearch = YouTubeQuery.SafeSearchValues.None;

			var request = new YouTubeRequest(new YouTubeRequestSettings(appName, "", apiKey));
			var feed = request.Get<Video>(query);
			return feed.Entries;

		}

		public Video GetVideoByID(string videoID)
		{
			var request = new YouTubeRequest(new YouTubeRequestSettings(appName, "", apiKey));
			Video video = null;
			if (videoID != null)
			{
				Uri videoEntryUrl = new Uri("http://gdata.youtube.com/feeds/api/videos/" + videoID);
				try
				{
					video = request.Retrieve<Video>(videoEntryUrl);
				}
				catch (Exception)
				{
					video = null;
				}
			}
			return video;
		}

		public ContentServiceResponse<Feed<Comment>> GetVideoComments(string videoID)
		{
			Video v = GetVideoByID(videoID);
			return GetVideoComments(v);
		}

		public ContentServiceResponse<Feed<Comment>> GetVideoComments(Video v)
		{
			var result = new ContentServiceResponse<Feed<Comment>>();

			try
			{
				var request = new YouTubeRequest(new YouTubeRequestSettings(appName, "", apiKey));
				result.Content = request.GetComments(v);
				result.ResultOption = ServiceResultOptions.Success;
			}
			catch (Exception ex)
			{
				result.SetException(ex);
			}

			return result;

		}

		public ServiceResponse AuthenticateUser(string username, string password)
		{
			try
			{
				string feedUrl = "http://gdata.youtube.com/feeds/api/users/default/inbox";
				YouTubeQuery query = new YouTubeQuery(feedUrl);
				var request = new YouTubeRequest(new YouTubeRequestSettings(appName, "", apiKey, username, password));
				var result = request.Service.GetMessages(query);
			}
			catch (Exception ex)
			{
				if (ex.Message == "Invalid credentials")
					return new ServiceResponse { ResultOption = ServiceResultOptions.ValidationFailure };
				else return new ServiceResponse { ResultOption = ServiceResultOptions.SystemException };
			}
			return new ServiceResponse { ResultOption = ServiceResultOptions.Success };
		}


		
		#endregion


	}

	public static class YouTubeExt
	{
		public static string ToString(this Video v)
		{
			return string.Format("ID:{0}; Views:{1}; Title:{2}; Duration:{3}", v.VideoId, v.ViewCount, v.Title, v.YouTubeEntry.Duration.Seconds);
		}
	}


}